package com.leyou.item.service;

import com.leyou.common.enums.ExceptionEnum;
import com.leyou.common.exceptions.LyException;
import com.leyou.common.utils.BeanHelper;
import com.leyou.item.dto.SpecGroupDTO;
import com.leyou.item.dto.SpecParamDTO;
import com.leyou.item.entity.SpecGroup;
import com.leyou.item.entity.SpecParam;
import com.leyou.item.mapper.SpecGroupMapper;
import com.leyou.item.mapper.SpecParamMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author 虎哥
 */
@Service
public class SpecService {

    @Autowired
    private SpecGroupMapper groupMapper;

    @Autowired
    private SpecParamMapper paramMapper;

    public List<SpecGroupDTO> queryGroupByCategory(Long id) {
        // 根据分类id查询规格组
        SpecGroup record = new SpecGroup();
        record.setCid(id);
        List<SpecGroup> list = groupMapper.select(record);
        // 非空判断
        if (CollectionUtils.isEmpty(list)) {
            throw new LyException(ExceptionEnum.SPEC_NOT_FOUND);
        }
        return BeanHelper.copyWithCollection(list, SpecGroupDTO.class);
    }

    public List<SpecParamDTO> queryParams(Long gid, Long cid, Boolean searching) {
        // 健壮性判断
        if (gid == null && cid == null && searching == null) {
            // 没有任何查询条件，不允许，返回404
            throw new LyException(ExceptionEnum.SPEC_NOT_FOUND);
        }
        // 根据group的id查询
        SpecParam record = new SpecParam();
        record.setGroupId(gid);
        record.setCid(cid);
        record.setSearching(searching);

        List<SpecParam> list = paramMapper.select(record);
        // 非空判断
        if (CollectionUtils.isEmpty(list)) {
            throw new LyException(ExceptionEnum.SPEC_NOT_FOUND);
        }
        return BeanHelper.copyWithCollection(list, SpecParamDTO.class);
    }

    public List<SpecGroupDTO> querySpecs(Long cid) {
        // 1.查询规格组
        List<SpecGroupDTO> specGroupDTOS = queryGroupByCategory(cid);

        // 2.查询分类下的所有参数
        List<SpecParamDTO> params = queryParams(null, cid, null);

        // 3.遍历params，根据groupId对param分组，形成 Map<Long, List<SpecParamDTO>>, key是groupId
        Map<Long, List<SpecParamDTO>> map = params.stream().collect(Collectors.groupingBy(SpecParamDTO::getGroupId));

        // 4.查询规格组中的参数
        for (SpecGroupDTO groupDTO : specGroupDTOS) {
            Long id = groupDTO.getId();
            // 根据组id，查询组内参数
            groupDTO.setParams(map.get(id));
        }
        return specGroupDTOS;


        /*Map<Long, List<SpecParamDTO>> map = new HashMap<>();
        for (SpecParamDTO param : params) {
            // 判断groupId是否存在
            if(!map.containsKey(param.getGroupId())){
                // 我是第一个，新建一个组
                map.put(param.getGroupId(), new ArrayList<>());
            }
            map.get(param.getGroupId()).add(param);
        }*/
    }
}
